package com.tzbank.voice.sys;

import java.util.List;

import com.tzbank.voice.entity.Config;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import com.tzbank.redis.RedisUtils;
import com.tzbank.utils.SpringContextUtils;
import com.tzbank.voice.dao.UtilsMapper;

/**
 * 启动初始化类
 *
 */
@Component
public class ApplicationRunner implements CommandLineRunner, ApplicationContextAware {

	@Autowired
	private UtilsMapper utilsMapper;

	@Override
	public void run(String... args) throws Exception {
		init();
	}

	/**
	 * 配置信息出初始化 放入redis 缓存
	 */
	private void init() {

		List<Config> VoiceConfig = utilsMapper.selectConfig();
		for (Config configvalue : VoiceConfig) {
			if ("apiKey".equalsIgnoreCase(configvalue.getConfigName())) {
				RedisUtils.setKey("VOICE_APIKEY", configvalue.getConfigValue());
			}
			if ("xAppid".equalsIgnoreCase(configvalue.getConfigName())) {
				RedisUtils.setKey("VOICE_XAPPID", configvalue.getConfigValue());
			}
			if ("url".equalsIgnoreCase(configvalue.getConfigName())) {
				RedisUtils.setKey("VOICE_URL", configvalue.getConfigValue());
			}

		}
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		SpringContextUtils.setApplicationContext(applicationContext);
	}
}
